<!-- 新增快照 -->
<template>
  <div>
    <a-modal
      :title="$t('Create')+$t('Snap')"
      v-model="modalVisible"
      width='30%'
      @ok='handleSubmit'
      @cancel="handleCancel"
      :maskClosable="false"
      v-dragModal
    >
      <a-form :form="form">
        <a-form-item :label="$t('Snapshot-name')" v-bind="formLayout" has-feedback>
          <a-input v-decorator="['snapshot_name',{
              rules:[
                {required:true,message:$t('Enter')+$t('Snapshot-name')},
                {pattern:namePattern,message:$t('Not_enter_special_characters')}
              ]
          }]" 
          :placeholder="$t('Enter')+$t('Snapshot-name')"
          allowClear>
          </a-input>
        </a-form-item>
      </a-form>
      <template slot="footer">
        <a-button @click="handleReset" class="submit-btn">
          {{$t('Reset')}}
        </a-button>
        <a-button
          class="submit-btn"
          type="primary"
          @click="handleSubmit"
          :loading="submitLoading"
        >
          {{$t('Sure')}}
        </a-button>
      </template>
    </a-modal>
  </div>
</template>

<script>
export default {
  name: "AddSnapshot",
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    targetBlock: {
      type: Object,
      required: true,
      default: () => { return {} }
    }
  },
  data() {
    return {
      modalVisible: false,
      submitLoading: false,
      form: this.$form.createForm(this),
      formLayout:{
        labelCol: {span: 6},
        wrapperCol: {span: 17}
      },
    }
  },

  watch: {
    visible(newValue) {
      this.modalVisible = newValue;
    }
  },

  methods: {
    //提交新增快照
    handleSubmit() {
      this.form.validateFields(async (err,value)=>{
        if(!err) {
          this.submitLoading = true;
          const params = {
            pool_id: this.targetBlock.pool_id,
            image_id: this.targetBlock.id,
            snapshot_name: value.snapshot_name,
          }
          try {
            const res = await this.$api.blockInfo.createSnapRequest(params)
            if(res) {
              this.model(res.data.id) //返回模板
              this.notice(res.data.id, res.data.params, res.data.flag) //通知列表显示
              if(res.data.flag == 0) {
                this.$emit('updateInfo')
              }
            }
          }catch(error) {
            this.$message.error(error);
          }
          this.handleCancel()
          this.submitLoading = false;
        }
      })
    },

    //关闭新增快照
    handleCancel() {
      this.$emit('closeOperationModal', 'add-snap');
      this.form.resetFields(); //重置弹窗
    },

    //重置操作
    handleReset() {
      this.form.resetFields(); //重置弹窗
    },
  },
}
</script>
<style scoped>
  .submit-btn {
    width: 70px;
  }
</style>